package com.etc.user.controller;/*
 * 文件名：com.etc.user.controller.UserPersonalController
 * 描述：用于用户操作用户个人中心
 * 作者：黄志鹏
 * 时间：2019/5/28 9:13
 */

import com.etc.entity.Orders;
import com.etc.entity.User;
import com.etc.service.UserPersonalService;
import com.etc.utils.CommonMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "user.action")
public class UserPersonalController {

    //注入UserPersonalService
    @Autowired
    private UserPersonalService userPersonalService;


    /**
     * 用户修改个人信息的方法
     * @param username 用户名
     * @param userpassword 用户密码
     * @param usersex 用户性别
     * @param userbirthday 用户生日
     * @return 是否修改成功
     */
    @RequestMapping("updateUserData")
    @ResponseBody
    public CommonMessage updataUserData(String username, String userpassword, String usersex, String userbirthday,Integer userid){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date iuserbirthday = simpleDateFormat.parse(userbirthday,new ParsePosition(0));

       //创建一个user对象
        User user = new User();
        user.setUsername(username);
        user.setUserpassword(userpassword);
        user.setUsersex(usersex);
        user.setUserbirthday(iuserbirthday);
        user.setUserid(userid);
        //创建一个返回值对象
        CommonMessage commonMessage = new CommonMessage();

        boolean flag = userPersonalService.updateUserDataByUserid(user);

        if (flag){
            //修改成功、设置返回值信息
            commonMessage.setMsg("修改成功");
        }else {
            //修改失败、设置返回值信息
            commonMessage.setMsg("修改失败");
        }
        return commonMessage;
    }

    /**
     * 跳转到个人信息页面的方法
     * @param model 存放数据的model
     * @return 个人信息页面的路径
     */
    @RequestMapping(value = "toInformation")
    public String toInformation(Integer userid ,Model model){
       User user= userPersonalService.getUserByUserid(userid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (user.getUserbirthday()!=null){
        String userbirthday = simpleDateFormat.format(user.getUserbirthday());
            model.addAttribute("userbirthday",userbirthday);
        }
       model.addAttribute("user1",user);

        return "user/information";
    }

    /**
     * 跳转至订单页面并携带数据
     * @param userid 用户的id
     * @param model 携带数据model
     * @return 返回订单页面的地址
     */
    @RequestMapping(value = "toOrder")
    public String  listOrdersByUserid(Integer userid , Model  model){

        List<Orders> orders=userPersonalService.listOrdersByUserid(userid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> listTime= new ArrayList<>();
        for (Orders orders2 : orders) {
                listTime.add(simpleDateFormat.format(orders2.getOrdertime()));
        }
        model.addAttribute("orders" ,orders);
        model.addAttribute("listTime" ,listTime);
        return "user/order";
    }

    /**
     * 确认收货的方法
     * @param orderid 订单的id
     * @return 返回是否成功
     */
    @RequestMapping(value = "confirmOrders")
    @ResponseBody
    public CommonMessage confirmOrders(Integer orderid){
        //创建一个返回值对象
        CommonMessage commonMessage =  new CommonMessage();
        boolean flag =userPersonalService.confirmOrders(orderid );
        if(flag){
            commonMessage.setMsg("收货成功");
        }else {
             commonMessage.setMsg("收货失败");
        }
        return commonMessage;
    }
    /**
     * 确认收货的方法
     * @param orderid 订单的id
     * @return 返回是否成功
     */
    @RequestMapping(value = "deleteOrders")
    @ResponseBody
    public CommonMessage deleteOrders(Integer orderid){
        //创建一个返回值对象
        CommonMessage commonMessage =  new CommonMessage();
        boolean flag =userPersonalService.deleteOrder(orderid );
        if(flag){
            commonMessage.setMsg("删除成功");
        }else {
            commonMessage.setMsg("删除失败");
        }
        return commonMessage;
    }

    @RequestMapping(value = "toOrderInfo")
    public String toOrderInfo( Integer orderid , Model model){

        Orders orders=userPersonalService.getOrdersByOrderid(orderid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String orderTime=  simpleDateFormat.format(orders.getOrdertime());
        model.addAttribute("orders",orders);
        model.addAttribute("orderTime",orderTime);
        return "user/orderInfo";
    }
}
